﻿using Factory.Common;
using Factory.MasterData.Materials;
using Factory.MasterData.Materials.Dto;
using Factory.MasterData.WorkshopSetups.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Factory.MasterData.WorkshopSetups
{
    public class WorkshopSetupAppService : ApplicationService, IWorkshopSetupAppService
    {
        private readonly IRepository<WorkshopSetup> repository;

        public WorkshopSetupAppService(IRepository<WorkshopSetup> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 新增工厂车间信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> CreateWorkshopSetup(AddWorkshopSetupDTO workshopSetupDTO)
        {
            //判断车间编号是否重复
            var workshoplist = await repository.GetListAsync(x => x.WorkShopCode == workshopSetupDTO.WorkShopCode);
            if (workshoplist.Count() > 0) return -1;

            var workshop = workshopSetupDTO.MapTo<WorkshopSetup>();
            await repository.InsertAsync(workshop);
            return 1;
        }

        /// <summary>
        /// 删除工厂车间信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteWorkshopSetup(int id)
        {
            await repository.DeleteAsync(x => x.Id == id);
            return 1;
        }

        /// <summary>
        /// 查询工厂车间信息
        /// </summary>
        /// <param name="WorkShopCode"></param>
        /// <param name="WorkShopName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<PagedResult> ShowWorkshopSetup(int page, int limit, string? workShopCode, string? workShopName)
        {
            var predicate= PredicateBuilder.New<WorkshopSetup>(true);
            if (!string.IsNullOrEmpty(workShopCode))
            {
                predicate = predicate.And(m => m.WorkShopCode.Contains(workShopCode));
            }
            if (!string.IsNullOrEmpty(workShopName))
            {
                predicate = predicate.And(m => m.WorkShopName.Contains(workShopName));
            }
            var workshoplist = repository.GetListAsync().Result.AsQueryable();
            PagedResult pageResult = new PagedResult();
            pageResult.RowCount = page;
            pageResult.PageCount = limit;
            pageResult.CurrentPage = workshoplist.Count();
            pageResult.Queryable = workshoplist.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return pageResult;
        }

        /// <summary>
        /// 根据id查询工厂车间信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<WorkshopSetupsDTO> GetByIdWorkshopSetup(int id)
        {
            var workshopSetups = await repository.FirstOrDefaultAsync(m => m.Id == id);
            var workshopSetupDTO = workshopSetups.MapTo<WorkshopSetupsDTO>();
            return workshopSetupDTO;
        }

        /// <summary>
        /// 修改工厂车间信息
        /// </summary>
        /// <param name="workshop"></param>
        /// <returns></returns>
        public async Task<int> UpdateWorkshopSetupStatus(WorkshopSetupsDTO workshopDTO)
        {
            //判断车间编号是否重复
            var workshoplist = repository.GetListAsync(x => x.Id!=workshopDTO.Id && x.WorkShopCode == workshopDTO.WorkShopCode).Result;
            if (workshoplist.Count() > 0)
                return -1;

            var workSetupDTO = workshopDTO.MapTo<WorkshopSetup>();
            await repository.UpdateAsync(workSetupDTO);
            return 1;
        }

    }


}
